package com.yxhl.zoume.core.coupon.adapter;

import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.yxhl.zoume.CouponSelectItemDataBinding;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.coupon.model.CouponSelectEvent;
import com.yxhl.zoume.core.coupon.model.CouponUseStatusEnum;
import com.yxhl.zoume.data.http.model.coupon.CouponInfo;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by vhawk on 2016/11/3.
 */
public class CouponSelectAdapter extends BaseAdapter<CouponSelectAdapter.CouponSelectViewHolder> {


    List<CouponInfo> selectedCoupons;

    public CouponSelectAdapter setSelectedCoupons(List<CouponInfo> selectedCoupons) {
        this.selectedCoupons = selectedCoupons;
        notifyDataSetChanged();
        return this;
    }

    @Override
    public CouponSelectAdapter.CouponSelectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CouponSelectItemDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_coupon_select, parent, false);
        CouponSelectAdapter.CouponSelectViewHolder holder = new CouponSelectAdapter.CouponSelectViewHolder(dataBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(CouponSelectAdapter.CouponSelectViewHolder holder, int position) {
        holder.bindTo(selectedCoupons.get(position));
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isEmpty(selectedCoupons)) {
            return 0;
        } else {
            return selectedCoupons.size();
        }
    }

    public class CouponSelectViewHolder extends BaseAdapter.BaseViewHolder {
        CouponSelectItemDataBinding dataBinding;

        public CouponSelectViewHolder(CouponSelectItemDataBinding dataBinding) {
            super(dataBinding.getRoot());
            this.dataBinding = dataBinding;
            ButterKnife.bind(this, dataBinding.getRoot());
        }

        public void bindTo(CouponInfo couponInfo) {
            dataBinding.setSelectedCouponInfo(couponInfo);
        }

        @OnClick
        public void itemClick() {
            CouponSelectEvent couponSelectEvent = new CouponSelectEvent();
            couponSelectEvent.setCouponId(selectedCoupons.get(getLayoutPosition()).getUserCouponId()+"")
            .setCouponUseStatusEnum(CouponUseStatusEnum.COUPON_USE_STATUS_ENUM_USED);
            RxBus.getInstance().send(couponSelectEvent);
        }
    }
}
